package com.wg.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wg.entity.User;
import com.wg.service.UserService;
import com.wg.utils.JsonObject;
import com.wg.utils.R;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageInfo;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @RequestMapping("/findownedWGById")
    public ArrayList findownedWGById(@RequestBody Integer userId) {
        return userService.findById(userId).ownedWG;
    }
    @RequestMapping("/findjoinedWGById")
    public ArrayList findjoinedWGById(@RequestBody Integer userId) {
        return userService.findById(userId).joinedWG;
    }
    @RequestMapping("/findfocusedWGById")
    public ArrayList findfocusedWGById(@RequestBody Integer userId) {
        return userService.findById(userId).focusedWG;
    }

    @RequestMapping("/queryUserAll")
    public JsonObject queryUserAll(@RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "15") Integer limit,
                                   User user) {
        JsonObject object = new JsonObject();
        PageInfo<User> pageInfo = userService.findUserAll(page, limit, user);
        object.setCode(0);
        object.setMsg("ok");
        object.setCount(pageInfo.getTotal());
        object.setData(pageInfo.getList());
        return object;
    }
    @ApiOperation(value = "删除")
    @RequestMapping("/deleteByIds")
    public R delete(String  ids){
        List<String> list= Arrays.asList(ids.split(","));
        //遍历遍历进行删除
        for(String id:list){
            userService.delete((int) Long.parseLong(id));
        }
        return R.ok();
    }


    @ApiOperation(value = "新增")
    @RequestMapping("/add")
    public R add(@RequestBody User user){
        userService.add(user);
        return R.ok();
    }


    @ApiOperation(value = "更新")
    @RequestMapping("/update")
    public R update(String oldPwd,String newPwd,Integer id){
        //根据id获取当前的数据记录
        User user=userService.findById(id);
        if(oldPwd.equals(user.password)){//输入的老密码和原密码一致
            user.password = (newPwd);
            userService.updateData(user);
            return R.ok();
        }else{
            return R.fail("两次密码不一致");
        }
    }

    @ApiOperation(value = "查询分页数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码"),
            @ApiImplicitParam(name = "pageCount", value = "每页条数")
    })
    @GetMapping()
    public IPage<User> findListByPage(@RequestParam Integer page,
                                          @RequestParam Integer pageCount){
        return userService.findListByPage(page, pageCount);
    }

    @ApiOperation(value = "id查询")
    @GetMapping("{id}")
    public User findById(@PathVariable Long id){
        return userService.findById(Math.toIntExact(id));
    }

}
